import { DataOccasion, field, occasion } from "../../coreModule/data/DataLoader";
import {BaseData, MainData, PK, pkType, StatusData} from "../../coreModule/data/BaseData";
import { DateUtils } from "../../../utils/DateUtils";
import { iLevelRepo } from "../../userModule/data/Role";

export enum TaskType {
	Military, Reward, PartTime, Camp
}

export enum TaskStatus {
	Paying = -2, Checking = -1, Normal, NotPassed,
	Started, Ended, Finished, Overdue, Deleted
}

const FeeRate = 0.05; // 手续费率
const FirstRate = 0.7; // 首款比例
const MaxDelayDay = 7; // 合同结束日期
const DelayFeeRate = 0.1; // 逾期费率
const ContractRate = 1.1; // 合同费率

export class Task extends StatusData<TaskStatus> {

	@field
	public type: TaskType = TaskType.Military;
	@field(String)
	public userId: string;
	@field(String)
	public title: string;
	@field(String)
	public location: string;
	@field(Number)
	public startTime: number;
	@field(Number)
	public endTime: number;
	@field(Number)
	public signStartTime: number;
	@field(Number)
	public signEndTime: number;
	@field(Number)
	public maxPayTime: number; // 最晚支付时间
	@field(Number)
	public count: number;
	@field(Number)
	public minCount: number;
	@field(Number)
	public level: number;
	@field(Number)
	public salary: number;
	@field(String)
	public phone: string;
	@field(String)
	public note: string;
	@field([String])
	public images: string[] = [];
	@field(String)
	public poster: string;

	@field(String)
	public name: string;

	@field(pkType())
	public firstTradeId?: PK;
	@field(pkType())
	public contractId?: PK;
	@field(pkType())
	public secondTradeId: PK;

	@field
	public isPaid: boolean = false;
	@field
	public isOverdue: boolean = false;

	@field(Number)
	public createdAt: number;
	@field
	public status: TaskStatus = TaskStatus.Paying;

	// region 额外信息

	@field(String)
	@occasion(DataOccasion.Extra)
	public startTimeStr: string;
	@field(String)
	@occasion(DataOccasion.Extra)
	public endTimeStr: string;
	@field(String)
	@occasion(DataOccasion.Extra)
	public signStartTimeStr: string;
	@field(String)
	@occasion(DataOccasion.Extra)
	public signEndTimeStr: string;

	@field(String)
	@occasion(DataOccasion.Extra)
	public levelStr: string;
	@field(String)
	@occasion(DataOccasion.Extra)
	public salaryStr: string;

	@field(String)
	// @occasion(DataOccasion.Extra)
	public userName: string;
	@field(String)
	// @occasion(DataOccasion.Extra)
	public userAvatar: string;

	@field(String)
	@occasion(DataOccasion.Extra)
	public statusStr: string;
	@field(Boolean)
	@occasion(DataOccasion.Extra)
	public isPaying: boolean;
	@field(Boolean)
	@occasion(DataOccasion.Extra)
	public isChecking: boolean;
	@field(Boolean)
	@occasion(DataOccasion.Extra)
	public isNormal: boolean;
	@field(Boolean)
	@occasion(DataOccasion.Extra)
	public isStarted: boolean;
	@field(Boolean)
	@occasion(DataOccasion.Extra)
	public isEnded: boolean;
	@field(Boolean)
	@occasion(DataOccasion.Extra)
	public isFinished: boolean;
	@field(Boolean)
	@occasion(DataOccasion.Extra)
	public isDeleted: boolean;

	@field(Boolean)
	@occasion(DataOccasion.Extra)
	public isMilitary: boolean; // 是否军事训练
	@field(Boolean)
	@occasion(DataOccasion.Extra)
	public isReward: boolean; // 是否悬赏活动
	@field(Boolean)
	@occasion(DataOccasion.Extra)
	public isPartTime: boolean; // 是否兼职活动
	@field(Boolean)
	@occasion(DataOccasion.Extra)
	public isCamp: boolean; // 是否成长训练营

	@field(Boolean)
	@occasion(DataOccasion.Extra)
	public isSignStarted: boolean; // 是否成长训练营

	/**
	 * 刷新数据
	 */
	public async refresh() {
		this.startTimeStr = DateUtils.time2Str(this.startTime, false);
		this.endTimeStr = DateUtils.time2Str(this.endTime, false);
		this.signStartTimeStr = DateUtils.time2Str(this.signStartTime, false);
		this.signEndTimeStr = DateUtils.time2Str(this.signEndTime, false);

		this.levelStr = this.level ?
			iLevelRepo().getByLevel(this.level).name : "无";
		this.salaryStr = (Number(this.salary) / 100).toFixed(2);
		this.statusStr = this.getStatusStr();

		this.isPaying = this.isStatus(TaskStatus.Paying);
		this.isChecking = this.isStatus(TaskStatus.Checking);
		this.isNormal = this.isStatus(TaskStatus.Normal);
		this.isStarted = this.isStatus(TaskStatus.Started);
		this.isEnded = this.isStatus(TaskStatus.Ended);
		this.isFinished = this.isStatus(TaskStatus.Finished);
		this.isDeleted = this.isStatus(TaskStatus.Deleted);

		this.isMilitary = this.type == TaskType.Camp; // Military;
		this.isReward = this.type == TaskType.Reward;
		this.isPartTime = this.type == TaskType.PartTime;
		this.isCamp = this.type == TaskType.Military;

		const now = Date.now();
		this.isSignStarted = this.signStartTime < now && now < this.signEndTime;
	}

	/**
	 * 获取状态文本
	 */
	public getStatusStr() {
		switch (this.status) {
			case TaskStatus.Paying: return "支付中";
			case TaskStatus.Checking: return "审核中";
			case TaskStatus.Normal: return "报名中";
			case TaskStatus.Started: return "已开始";
			case TaskStatus.Finished: return "已完成";
			case TaskStatus.Overdue: return "已逾期";
			case TaskStatus.Deleted: return "已删除";
			case TaskStatus.NotPassed: return "审核不通过";
			default: return "未知状态";
		}
	}

	// endregion

	// region 数据获取

	/**
	 * 总薪水
	 */
	public get totalPrice() {
		return this.salary * this.count *
			Math.max(DateUtils.dayDiff(this.endTime, this.startTime), 1);
	}

	/**
	 * 首款金额
	 */
	public get firstPrice() {
		return Math.ceil(this.totalPrice * FirstRate);
	}

	/**
	 * 尾款金额
	 */
	public get secondPrice() {
		if (this.contractId)
			return this.totalPrice * ContractRate;
		return this.totalPrice - this.firstPrice;
	}

	/**
	 * 最终每人日薪
	 */
	public get finalSalary() {
		return this.salary * FeeRate;
	}

	// endregion

	/**
	 * 测试数据
	 */
	public static testData() {
		const res = new Task();
		res._id = "123456789";
		res.title = "测试标题";
		res.type = TaskType.Military;
		res.location = "北京市 朝阳区 太行西街道319号";
		res.startTime = Date.now();
		res.endTime = Date.now() + DateUtils.Day * 5;
		res.count = 12;
		res.level = 2;
		res.note = "至少都在178以上，高中以上学历";
		res.salary = 1000;
		res.phone = "15918881888";

		res.refresh();

		res.userName = "测试公司"

		return res;
	}
}

const TypeNames = ["成长训练营", "场地", "日常兼职", "军事训练"];

export class TaskIndexData extends BaseData {

	@field(pkType())
	public _id: PK;
	@field(String)
	public userId: string;
	@field(String)
	public userName: string;
	@field(String)
	public userAvatar: string;
	@field(Number)
	public type: TaskType;
	@field(String)
	public title: string;
	@field(String)
	public location: string;
	@field(Number)
	public level: number;
	@field(Number)
	public count: number;
	@field(Number)
	public particCount: number;
	@field(Number)
	public salary: number;
	@field(String)
	public note: string;
	@field(Number)
	public startTime: number;
	@field(Number)
	public endTime: number;
	@field(Number)
	public createdAt: number;

	// region 额外信息

	@field(String)
	@occasion(DataOccasion.Extra)
	public timeStr: string;
	@field([String])
	@occasion(DataOccasion.Extra)
	public tags: string[];
	@occasion(DataOccasion.Extra)
	public salaryStr: string;

	@field(Boolean)
	@occasion(DataOccasion.Extra)
	public isMilitary: boolean; // 是否军事训练
	@field(Boolean)
	@occasion(DataOccasion.Extra)
	public isReward: boolean; // 是否悬赏活动
	@field(Boolean)
	@occasion(DataOccasion.Extra)
	public isPartTime: boolean; // 是否兼职活动
	@field(Boolean)
	@occasion(DataOccasion.Extra)
	public isCamp: boolean; // 是否成长训练营

	public refresh() {
		this.timeStr = DateUtils.time2Str(this.createdAt);
		this.salaryStr = (Number(this.salary) / 100).toFixed(2);

		this.isMilitary = this.type == TaskType.Camp;
		this.isReward = this.type == TaskType.Reward;
		this.isPartTime = this.type == TaskType.PartTime;
		this.isCamp = this.type == TaskType.Military;

		this.refreshTags();
	}

	public refreshTags() {
		this.tags = [];
		this.tags.push(TypeNames[this.type]);

		if (this.isMilitary) {
			const level = iLevelRepo().getByLevel(this.level || 1);
			this.tags.push(level.name);
		}
		if(!this.isReward) this.tags.push(this.count + "人");
		const day = Math.max(DateUtils.dayDiff(this.endTime, this.startTime), 1);
		this.tags.push(day + "天");
	}

	//endregion

	/**
	 * 测试数据
	 */
	public static testData() {
		const res = new TaskIndexData();
		res._id = "123456789";
		res.title = "军训，需要3位教官";
		res.createdAt = Date.now();
		res.userAvatar = "https://img1.baidu.com/it/u=3008696966,846351368&fm=26&fmt=auto";
		res.userName = "北京科技大学";
		res.location = "北京市 朝阳区 太行西街道";
		res.salary = 300;
		res.tags = ["学校军训", "D级", "3人", "12天"];

		res.refresh();

		const res1 = new TaskIndexData();
		res1._id = "1234567891";
		res1.title = "军训，需要3位教官";
		res1.createdAt = res.createdAt + 100000000
		res1.userAvatar = "https://img1.baidu.com/it/u=3008696966,846351368&fm=26&fmt=auto";
		res1.userName = "北京科技大学";
		res1.location = "北京市 朝阳区 太行西街道";
		res1.salary = 3000;
		res1.tags = ["学校军训", "D级", "3人", "12天"];

		return [res, res1, res, res1, res];
	}
}
